﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BDSim
{
    /// <summary>
    /// 卫星数据交互模型
    /// </summary>
    public class SatelliteDataProvider : BDSimDataProvider
    {
        private Satellite _Parent;
        /// <summary>
        /// 数据模型的父级
        /// </summary>
        public Satellite Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
        
        #region 命令定义

        /// <summary>
        /// 星座设计
        /// </summary>
        MenuCommand DesignConstellationCmd = new MenuCommand();

        /// <summary>
        /// 力模型设置
        /// </summary>
        MenuCommand ForceSetCmd = new MenuCommand();

        /// <summary>
        /// 积分器设置
        /// </summary>
        MenuCommand IntegrationSetCmd = new MenuCommand();
        
        /// <summary>
        /// 钟差数据
        /// </summary>
        MenuCommand ClockDataCmd = new MenuCommand();

        /// <summary>
        /// 钟差报告
        /// </summary>
        MenuCommand ClockDataReportCmd = new MenuCommand();

        /// <summary>
        /// 钟差图表
        /// </summary>
        MenuCommand ClockDataGraphicCmd = new MenuCommand();

        /// <summary>
        /// 轨道数据
        /// </summary>
        MenuCommand OrbitDataCmd = new MenuCommand();

        /// <summary>
        /// 理论轨道
        /// </summary>
        MenuCommand ReferOrbitCmd = new MenuCommand();

        /// <summary>
        /// 理论轨道报告
        /// </summary>
        MenuCommand ReferOrbitReportCmd = new MenuCommand();

        /// <summary>
        /// 理论轨道图表
        /// </summary>
        MenuCommand ReferOrbitGraphicCmd = new MenuCommand();

        ///// <summary>
        ///// 自主定轨
        ///// </summary>
        //MenuCommand AutoODOrbitCmd = new MenuCommand();

        ///// <summary>
        ///// 自主定轨报告
        ///// </summary>
        //MenuCommand AutoODOrbitReportCmd = new MenuCommand();

        ///// <summary>
        ///// 自主定轨图表
        ///// </summary>
        //MenuCommand AutoODOrbitGraphicCmd = new MenuCommand();

        ///// <summary>
        ///// 观测数据
        ///// </summary>
        //MenuCommand ObsDataCmd = new MenuCommand();

        ///// <summary>
        ///// 星间观测数据
        ///// </summary>
        //MenuCommand Sat2SatObsCmd = new MenuCommand();

        ///// <summary>
        ///// 星间观测数据报告
        ///// </summary>
        //MenuCommand Sat2SatObsReportCmd = new MenuCommand();

        ///// <summary>
        ///// 星地Ka上行观测数据
        ///// </summary>
        //MenuCommand Sat2StaKaUpObsCmd = new MenuCommand();

        ///// <summary>
        /////  星地Ka上行观测数据报告
        ///// </summary>
        //MenuCommand Sat2StaKaUpObsReprotCmd = new MenuCommand();

        ///// <summary>
        ///// 星地L上行观测数据
        ///// </summary>
        //MenuCommand Sat2StaLUpObsCmd = new MenuCommand();

        ///// <summary>
        /////  星地L上行观测数据报告
        ///// </summary>
        //MenuCommand Sat2StaLUpObsReprotCmd = new MenuCommand();


        /// <summary>
        /// 删除卫星
        /// </summary>
        MenuCommand DeleteSatelliteCmd = new MenuCommand();

        #endregion

        #region 命令集合初始化

        public override void DefineCommand()
        {
            DesignConstellationCmd.Header = "生成Walker星座";
            DesignConstellationCmd.DefineExcuteCommand(DesignConstellationFun_Execute, DesignConstellationFun_CanExecute);
            MenuCommandList.Add(DesignConstellationCmd);

            ForceSetCmd.Header = "力模型";
            ForceSetCmd.DefineExcuteCommand(ForceSetFun, CmdForceSetFun_CanExecute);
            MenuCommandList.Add(ForceSetCmd);

            IntegrationSetCmd.Header = "积分器";
            IntegrationSetCmd.DefineExcuteCommand(IntegrationSetFun, CmdForceSetFun_CanExecute);
            MenuCommandList.Add(IntegrationSetCmd);

            #region 注册轨道数据输出命令

            //轨道数据报告
            ReferOrbitReportCmd.Header = "生成报告";
            ReferOrbitReportCmd.DefineExcuteCommand(ReferOrbitReportCmd_Execute, ReferOrbitReportCmd_CanExecute);
            ReferOrbitCmd.SubMenuCommandList.Add(ReferOrbitReportCmd);

            //轨道图表
            ReferOrbitGraphicCmd.Header = "生成图表";
            ReferOrbitGraphicCmd.DefineExcuteCommand(ReferOrbitGraphicCmd_Execute, ReferOrbitGraphicCmd_CanExecute);
            ReferOrbitCmd.SubMenuCommandList.Add(ReferOrbitGraphicCmd);
            
            ReferOrbitCmd.Header = "理论轨道";
         //   OrbitDataCmd.SubMenuCommandList.Add(ReferOrbitCmd);
            MenuCommandList.Add(ReferOrbitCmd);
            ////自主定轨结果数据报告
            //AutoODOrbitReportCmd.Header = "生成报告";
            //AutoODOrbitReportCmd.DefineExcuteCommand(AutoODOrbitReportCmd_Execute, AutoODOrbitReportCmd_CanExecute);
            //AutoODOrbitCmd.SubMenuCommandList.Add(AutoODOrbitReportCmd);
            ////自主定轨图形化结果
            //AutoODOrbitGraphicCmd.Header = "生成图表";
            //AutoODOrbitGraphicCmd.DefineExcuteCommand(AutoODOrbitReportCmd_Execute, AutoODOrbitReportCmd_CanExecute);
            //AutoODOrbitCmd.SubMenuCommandList.Add(AutoODOrbitGraphicCmd);

            //AutoODOrbitCmd.Header = "自主定轨";
            //OrbitDataCmd.SubMenuCommandList.Add(AutoODOrbitCmd);

            //OrbitDataCmd.Header = "轨道";
            //MenuCommandList.Add(OrbitDataCmd);
            #endregion

            ClockDataReportCmd.Header = "生成报告";
            ClockDataReportCmd.DefineExcuteCommand(ClockDataReportCmd_Execute, ClockDataReportCmd_CanExecute);
            ClockDataCmd.SubMenuCommandList.Add(ClockDataReportCmd);

            ClockDataGraphicCmd.Header = "生成图表";
            ClockDataGraphicCmd.DefineExcuteCommand(ClockDataGraphicCmd_Execute, ClockDataGraphicCmd_CanExecute);
            ClockDataCmd.SubMenuCommandList.Add(ClockDataGraphicCmd);

            ClockDataCmd.Header = "钟差";
            MenuCommandList.Add(ClockDataCmd);

            //Sat2SatObsReportCmd.Header = "生成报告";
            //Sat2SatObsCmd.SubMenuCommandList.Add(Sat2SatObsReportCmd);

            //Sat2SatObsCmd.Header = "星间观测数据";
            //ObsDataCmd.SubMenuCommandList.Add(Sat2SatObsCmd);

            //Sat2StaKaUpObsReprotCmd.Header = "生成报告";
            //Sat2StaKaUpObsCmd.SubMenuCommandList.Add(Sat2StaKaUpObsReprotCmd);

            //Sat2StaKaUpObsCmd.Header = "星地上行观测数据";
            //ObsDataCmd.SubMenuCommandList.Add(Sat2StaKaUpObsCmd);

            //ObsDataCmd.Header = "观测数据";
            //MenuCommandList.Add(ObsDataCmd);

            DeleteSatelliteCmd.Header = "删除";
            DeleteSatelliteCmd.DefineExcuteCommand(DeleteSatelliteFun, CmdForceSetFun_CanExecute);
            MenuCommandList.Add(DeleteSatelliteCmd);
        }

        #endregion

        #region 命令执行内容定义

        #region 星座设计
        /// <summary>
        /// 星座设计的执行方法
        /// </summary>
        void DesignConstellationFun_Execute(object param)
        {
            //Walker星座的卫星坐标数据
            NodePosVel[] Satoutput;

            //Walker星座的卫星轨道数据
            NodePosVel[] Trackoutput;
            NodePosVel SeedPosVel;
            SeedPosVel = Parent.Orbit.InitPV_ECI.ConvertToStruct();
            WalkerSatConstWnd  WndWalkerSat = new WalkerSatConstWnd();
            WndWalkerSat.OrbitP = SeedPosVel;
            if (WndWalkerSat.ShowDialog() == true)
            {
                int WalkerSat_T = WndWalkerSat.SatDesignVM.WalkerSat_T;
                int WalkerSat_P = WndWalkerSat.SatDesignVM.WalkerSat_P;
                int WalkerSat_F = WndWalkerSat.SatDesignVM.WalkerSat_F;

                NodePosVel SeedSat = new NodePosVel();
                SeedSat.epoch = BDSimObject.GlobalStartTime;
                SeedSat.x = WndWalkerSat.SatDesignVM.MEOECIOrbit.X;
                SeedSat.y = WndWalkerSat.SatDesignVM.MEOECIOrbit.Y;
                SeedSat.z = WndWalkerSat.SatDesignVM.MEOECIOrbit.Z;

                SeedSat.vx = WndWalkerSat.SatDesignVM.MEOECIOrbit.VX;
                SeedSat.vy = WndWalkerSat.SatDesignVM.MEOECIOrbit.VY;
                SeedSat.vz = WndWalkerSat.SatDesignVM.MEOECIOrbit.VZ;
                           
                ConstellationDLL.WalkerConst(SeedSat, WalkerSat_T, WalkerSat_P, WalkerSat_F, out Satoutput, out Trackoutput);

                Parent.Orbit.InitPV_ECI = SeedSat.ConvertToClass();
                for (int i = 1; i < WalkerSat_T; i++)
                {
                    Satellite Sat = new Satellite();
                    Sat.Type = ESpaceType.Satellite;

                    Sat.Orbit.InitPV_ECI = Satoutput[i].ConvertToClass();
                    Sat.Parent = this.Parent.Parent;
                    Sat.Id = Sat.Parent.SatelliteCollection.Count+1;
                  //  Sat.Name = Sat.Parent.Name + (i + 1).ToString();
                    Sat.Name = "Satellite" + (Sat.Parent.SatelliteCollection.Count + 1).ToString();
                    
                    this.Parent.Parent.SatelliteCollection.Add(Sat);
                    
                }
            }
        }
        bool DesignConstellationFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 力模型设计
        /// <summary>
        /// 力模型设置的执行方法
        /// </summary>
        void ForceSetFun(object param)
        {
            ForceWnd forceWnd = new ForceWnd();
            forceWnd.ForceVM.COrbitPredictParam = this.Parent.Orbit.COrbitPredictParam;
            //forceWnd.DataContext = this.Parent.Orbit;
            if (forceWnd.ShowDialog()==true)
            {
                this.Parent.Orbit.COrbitPredictParam = forceWnd.ForceVM.COrbitPredictParam;
            }            
        }

        bool CmdForceSetFun_CanExecute(object o)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 积分器设计

        /// <summary>
        /// 积分器设置的执行方法
        /// </summary>
        void IntegrationSetFun(object param)
        {
            IntegratorWnd Integrator = new IntegratorWnd();
            Integrator.IntegratorVM.IntegrationParam = this.Parent.Orbit.IntegrationParam;
           // Integrator.DataContext = this.Parent.Orbit;
            if (Integrator.ShowDialog()==true)
            {
                this.Parent.Orbit.IntegrationParam = Integrator.IntegratorVM.IntegrationParam;
            }
           
        }
        #endregion

        #region 轨道数据报告、图表执行方法
        void ReferOrbitReportCmd_Execute(object param)
        {
            DataReportView dataPreview = new DataReportView();
           // dataPreview.InitOrbitData(Parent.InterOrbitPVList, Parent.Name);
            List<OrbitSimData> InterOrbitPVList = Parent.GenerateStepOrbit();
            dataPreview.InitOrbitData(InterOrbitPVList, Parent.Name);
            MainDispatcher.DlgAddLayoutDocument(dataPreview, Parent.Name + "_轨道报告");
        }

        bool ReferOrbitReportCmd_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Inited || BDSimObject.GlobalStatus == ESimStatus.Pause)
            {
                flag = true;
            }
            return flag;
        }

        void ReferOrbitGraphicCmd_Execute(object param)
        {
            DataFigureView orbitReportView = new DataFigureView();
          //  orbitReportView.InitOrbitCharts(Parent.Name, Parent.InterOrbitPVList);
            List<OrbitSimData> InterOrbitPVList = Parent.GenerateStepOrbit();
            orbitReportView.InitOrbitCharts(Parent.Name, InterOrbitPVList);
            MainDispatcher.DlgAddLayoutDocument(orbitReportView, Parent.Name + "_轨道图表");
        }

        bool ReferOrbitGraphicCmd_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Inited || BDSimObject.GlobalStatus == ESimStatus.Pause)
            {
                flag = true;
            }
            return flag;
        }

        void AutoODOrbitReportCmd_Execute(object param)
        {
            DataFigureView orbitReportView = new DataFigureView();
          //  orbitReportView.InitOrbitCharts(Parent.Name, Parent.InterOrbitPVList);
            MainDispatcher.DlgAddLayoutDocument(orbitReportView, Parent.Name + "_轨道数据");
        }

        bool AutoODOrbitReportCmd_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Inited || BDSimObject.GlobalStatus == ESimStatus.Pause)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 钟差数据报告、图表执行方法
        void ClockDataReportCmd_Execute(object param)
        {
            DataReportView dataPreview = new DataReportView();
            List<ClkSimData> InterSatClkErrList = new List<ClkSimData>();
            InterSatClkErrList = Parent.GenerateStepCLKErr((int)BDSimObject.GlobalStepSize);
            dataPreview.InitClkData(InterSatClkErrList, Parent.Name);
            MainDispatcher.DlgAddLayoutDocument(dataPreview, Parent.Name + "_钟差数据");
        }

        bool ClockDataReportCmd_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Inited || BDSimObject.GlobalStatus == ESimStatus.Pause)
            {
                flag = true;
            }
            return flag;
        }

        void ClockDataGraphicCmd_Execute(object param)
        {
            DataFigureView clkFigureView = new DataFigureView();
            List<ClkSimData> InterSatClkErrList = new List<ClkSimData>();
            InterSatClkErrList = Parent.GenerateStepCLKErr((int)BDSimObject.GlobalStepSize);
            clkFigureView.InitClkCharts(Parent.Name, InterSatClkErrList);
           // clkFigureView.InitClkCharts(Parent.Name, Parent.InterSatClkErrList);
            MainDispatcher.DlgAddLayoutDocument(clkFigureView, Parent.Name + "_钟差图表");            
        }

        bool ClockDataGraphicCmd_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Inited || BDSimObject.GlobalStatus == ESimStatus.Pause)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 删除卫星
        /// <summary>
        /// 删除卫星的执行方法
        /// </summary>
        void DeleteSatelliteFun(object param)
        {
            Parent.Parent.SatelliteCollection.Remove(Parent);
        }
        #endregion

        #endregion

    }
}
